package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.mapper.bean.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//@ResponseBody
//@Controller
@RestController //@RestController的作用相当于是@ResponseBody+@Controller，但是如果类中含有不用传json数据的方法时，则不能使用这个注解
public class RoleHandler {

    @Autowired
    private RoleService roleService;



    @PreAuthorize("hasRole('研究员')")
    //查询role对象
    //@ResponseBody //如果类中每一个方法都有@ResponseBody注解，则可以将它加在类名上面
    @RequestMapping("/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo (
            @RequestParam(value = "keyword", defaultValue = "") String Keyword,
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize
    ){

        // 1.获取service中的pageInfo对象
        PageInfo<Role> pageInfo = roleService.getPageInfo(Keyword, pageNum, pageSize);
        System.out.println("<=============="+Keyword+"===============>");
        // 2.封装到ResultEntity对象中返回
        // （如上面的操作抛出异常，交给我们自己写的异常机制CrowdExceptionResolver类进行处理）
        return ResultEntity.successWithData(pageInfo);
    }

    //添加role对象
    //@ResponseBody //如果类中每一个方法都有@ResponseBody注解，则可以将它加在类名上面
    @RequestMapping("/role/save.json")
    public ResultEntity<String> saveRole(Role role) {

        roleService.saveRole(role);

        return ResultEntity.successWithoutData();
    }

    //修改role对象
    //@ResponseBody //如果类中每一个方法都有@ResponseBody注解，则可以将它加在类名上面
    @RequestMapping("/role/update.json")
    public ResultEntity<String> updateRole(Role role) {
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    //删除role对象
    //@ResponseBody //如果类中每一个方法都有@ResponseBody注解，则可以将它加在类名上面
    @RequestMapping("/role/remove/by/role/id/array.json")
    public ResultEntity<String> removeRole(@RequestBody List<Integer> roleIdList) {
        roleService.removeRole(roleIdList);
        return ResultEntity.successWithoutData();
    }
}